package com.assignwork.modules.pc.service.pc.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.assignwork.dto.ShipForecastSearchDTO;
import com.assignwork.dto.pcplan.PcShipRealWritingSearchDto;
import com.assignwork.dto.pcplan.PcShipRealWritingSubmitDto;
import com.assignwork.dto.pcplan.SyncPcShipRealWritingDto;
import com.assignwork.entity.pc.AttachmentDetails;
import com.assignwork.entity.pc.PcShipEfficiency;
import com.assignwork.entity.pc.PcShipRealWriting;
import com.assignwork.mapper.PcShipRealWritingMapper;
import com.assignwork.modules.pc.service.pc.IPcShipRealWritingService;
import com.assignwork.service.ICommonService;
import com.assignwork.vo.pc.PcShipRealWritingPageVo;
import com.assignwork.vo.pc.PcShipRealWritingVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName PcShipRealWritingServiceImpl
 * @Description
 * @Author sl
 * @Date 2023/12/27 10:25
 */
@Service
@AllArgsConstructor
public class PcShipRealWritingServiceImpl extends BaseServiceImpl<PcShipRealWritingMapper, PcShipRealWriting> implements IPcShipRealWritingService {
    //    private IAttachService attachService;
//    private IAttachClient iAttachClient;
    private ICommonService iCommonService;
    private PcShipRealWritingMapper pcShipRealWritingMapper;
    @Override
    public boolean submitPcShipRealWriting(PcShipRealWritingSubmitDto submitDTO) {
//        pcShipRealWritingService
        return super.saveOrUpdate(submitDTO);
    }

    @Override
    public boolean syncPcShipRealWriting(SyncPcShipRealWritingDto dto) {
        JSONArray jsonArrays = new JSONArray();
//        LambdaQueryChainWrapper<PcShipRealWriting> eq = super.lambdaQuery().eq(PcShipRealWriting::getId, dto.getId());
//        PcShipRealWriting one = eq.one();
        List<PcShipRealWritingVo> pcShipRealWritingVos = baseMapper.selectSyncPcShipRealWriting(String.valueOf(dto.getId()));

        pcShipRealWritingVos.forEach(e -> {
            List<AttachmentDetails> attachmentDetailslist = (List<AttachmentDetails>) e.getAttachmentDetails();
            String s = JSON.toJSONString(attachmentDetailslist);
            attachmentDetailslist = JSON.parseArray(s, AttachmentDetails.class);
            attachmentDetailslist.forEach(es -> {
                JSONObject attachmentDetails = new JSONObject();
                JSONObject single = new JSONObject();
                single.put("name", DictBizCache.getValue("hatch_type", es.getHatch()));
                if (es.getOssId().equals(dto.getOssId())) {
                    attachmentDetails.put("isSyncShift", dto.getIsSyncShift());
                }else{
                    attachmentDetails.put("isSyncShift", es.getIsSyncShift());
                }
                attachmentDetails.put("url", es.getUrl());
                attachmentDetails.put("hatch", es.getHatch());
                attachmentDetails.put("ossId", String.valueOf(es.getOssId()));
                attachmentDetails.put("file", single);
                jsonArrays.add(attachmentDetails);
            });
            PcShipRealWriting shipRealWriting = super.lambdaQuery().eq(PcShipRealWriting::getId, dto.getId()).one();
            shipRealWriting.setAttachmentDetails(jsonArrays);
            baseMapper.updateById(shipRealWriting);
//            super.saveOrUpdate(shipRealWriting);
        });
        return true;
    }

    @Override
    public boolean clearSelectedPcShipRealWriting(SyncPcShipRealWritingDto dto) {
        List<PcShipRealWritingVo> pcShipRealWritingVos = baseMapper.selectAllPcShipRealWriting(String.valueOf(dto.getShipId()));
        System.out.println(pcShipRealWritingVos);
        pcShipRealWritingVos.forEach(e -> {
            JSONArray jsonArrays = new JSONArray();
            List<AttachmentDetails> attachmentDetailslist = (List<AttachmentDetails>) e.getAttachmentDetails();
            String s = JSON.toJSONString(attachmentDetailslist);
            attachmentDetailslist = JSON.parseArray(s, AttachmentDetails.class);
            attachmentDetailslist.forEach(es -> {
                JSONObject attachmentDetails = new JSONObject();
                JSONObject single = new JSONObject();
                single.put("name", DictBizCache.getValue("hatch_type", es.getHatch()));
                attachmentDetails.put("isSyncShift", dto.getIsSyncShift());
                attachmentDetails.put("url", es.getUrl());
                attachmentDetails.put("hatch", es.getHatch());
                attachmentDetails.put("ossId", String.valueOf(es.getOssId()));
                attachmentDetails.put("file", single);
                jsonArrays.add(attachmentDetails);
            });
            PcShipRealWriting shipRealWriting = super.lambdaQuery().eq(PcShipRealWriting::getId, e.getId()).one();
            shipRealWriting.setAttachmentDetails(jsonArrays);
            baseMapper.updateById(shipRealWriting);
        });
        return true;
    }

    @Override
    public List<PcShipRealWritingVo> selectPcShipRealWritingEasyList(PcShipRealWritingSearchDto dto) {
        List<PcShipRealWritingVo> pcShipRealWritingVos = baseMapper.selectPcShipRealWritingList(dto);
        pcShipRealWritingVos.forEach(e -> {
            List<AttachmentDetails> attachmentDetailslist = (List<AttachmentDetails>) e.getAttachmentDetails();
            String s = JSON.toJSONString(attachmentDetailslist);
            attachmentDetailslist = JSON.parseArray(s, AttachmentDetails.class);
            attachmentDetailslist.forEach(es -> {
                es.setHatchName(DictBizCache.getValue("hatch_type", es.getHatch()));
            });
            e.setAttachmentDetails(attachmentDetailslist);
        });
        return pcShipRealWritingVos;
    }

    @Override
    public List<PcShipRealWritingVo> selectPcShipRealWritingList(PcShipRealWritingSearchDto dto) {
        List<PcShipRealWritingVo> pcShipRealWritingVos = baseMapper.selectPcShipRealWritingList(dto);
        pcShipRealWritingVos.forEach(e -> {
            e.setUpdateUserName(iCommonService.userName(String.valueOf(e.getUpdateUser())));
            List<AttachmentDetails> attachmentDetailslist = (List<AttachmentDetails>) e.getAttachmentDetails();
            String s = JSON.toJSONString(attachmentDetailslist);
            attachmentDetailslist = JSON.parseArray(s, AttachmentDetails.class);
            attachmentDetailslist.forEach(es -> {
                es.setHatchName(DictBizCache.getValue("hatch_type", es.getHatch()));
                es.setUrl(es.getUrl().replaceAll("http://10.10.21.252:9000", "https://www.tjportnofour.com/oss"));
            });
            e.setAttachmentDetails(attachmentDetailslist);
        });
        return pcShipRealWritingVos;
    }

    @Override
    public IPage<PcShipRealWritingPageVo> selectPcShipRealWritingPage(ShipForecastSearchDTO dto, IPage<PcShipRealWritingPageVo> page) {
        List<PcShipRealWritingPageVo> data = baseMapper.selectPcShipRealWritingPage(dto, page);
        for (PcShipRealWritingPageVo vo : data) {
            if (!Func.isNull(vo.getForecastStatus())) {
                vo.setForecastStatusName(DictBizCache.getValue("forecast_status", vo.getForecastStatus()));
            }
            if (!Func.isNull(vo.getBerth())) {
                vo.setBerthName(DictBizCache.getValue("berth", vo.getBerth()));
            }
            if (!Func.isNull(vo.getTradeType())) {
                vo.setTradeTypeName(vo.getTradeType().equals("1") ? "外贸" : "内贸");
            }

        }
        return page.setRecords(data);
    }
}
